
Imports System.Data.SqlClient
Imports System.Console
Imports System.IO.TextReader
Imports System.IO
Imports System.Net
Imports System.IO.Ports
Imports System.Text
Imports System.Xml
Imports System.IO.FileStream

Imports System.Threading
Imports System.Collections


Public Class GPSDevice

    Private WithEvents SerialPort As New IO.Ports.SerialPort

    Private mTrama As String


    Public Property Trama()
        Get
            Return mTrama
        End Get
        Set(ByVal value)
            mTrama = value
        End Set
    End Property


    Public Function ReadData(ByVal ComPort As String) As Stack

        If SerialPort.IsOpen = False Then

            Try
                With SerialPort
                    .PortName = ComPort
                    .BaudRate = 4800
                    .Parity = IO.Ports.Parity.None
                    .DataBits = 8
                    .StopBits = IO.Ports.StopBits.One
                    .Handshake = IO.Ports.Handshake.None
                End With
                SerialPort.Open()

            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try

        End If

        Dim GPSTramaDatos As String = SerialPort.ReadExisting
        Dim GPSLineasDatos() As String = Split(GPSTramaDatos, vbCrLf)

        Dim PilaDataGPS As New Stack

        For i As Integer = 0 To GPSLineasDatos.Length - 1

            Dim GeoTrama As String = GPSLineasDatos(i)

            If GeoTrama.Length > 3 Then
                If GeoTrama.Substring(0, 3) = "$GP" And GeoTrama.Contains("*") Then

                    PilaDataGPS.Push(GeoTrama)

                End If
            End If

        Next

        Return PilaDataGPS

    End Function


End Class
